#include "tree_display.h"
#include <qdebug.h>
#include <qpushbutton.h>
void set_button_pos(QPushButton* p, int x, int y) {
    p->move(x, y);
}
void set_button_size(QPushButton* p, int x, int y) {
    p->resize(x, y);
}
tree_display::tree_display(QWidget *parent)
    : QMainWindow(parent)
{
    tree = nullptr;
    ui.setupUi(this);
    QPushButton* b_binary = new QPushButton("Binary Tree", this);
    QPushButton* b_Bserch = new QPushButton("BSerch Tree", this);
    QPushButton* b_AVL = new QPushButton("AVL Tree", this);
    QPushButton* b_RB = new QPushButton("RB Tree", this);
    set_button_size(b_binary, 100, 30);
    set_button_size(b_Bserch, 100, 30);
    set_button_size(b_AVL, 100, 30);
    set_button_size(b_RB, 100, 30);
    set_button_pos(b_binary, 100, 100);
    set_button_pos(b_Bserch, 100, 150);
    set_button_pos(b_AVL, 100, 200);
    set_button_pos(b_RB, 100, 250);
    QPushButton* b_insert = new QPushButton("insert", this);
    QPushButton* b_delete = new QPushButton("delete", this);
    set_button_pos(b_insert, 1500, 200);
    set_button_pos(b_delete, 1500, 250);
    QObject::connect(b_binary, SIGNAL(clicked()), this, SLOT(Binary_init()));
    QObject::connect(b_Bserch, SIGNAL(clicked()), this, SLOT(Bserch_init()));
    QObject::connect(b_AVL, SIGNAL(clicked()), this, SLOT(AVL_init()));
    QObject::connect(b_RB, SIGNAL(clicked()), this, SLOT(RBTree_init()));
    QObject::connect(b_insert, SIGNAL(clicked()), this, SLOT(b_insert()));
    QObject::connect(b_delete, SIGNAL(clicked()), this, SLOT(b_delete()));
    q_insert = new QLineEdit(this);
    q_insert->move(1610, 200);
    q_insert->resize(100, 30);
    q_delete = new QLineEdit(this);
    q_delete->move(1610, 250);
    q_delete->resize(100, 30);
}
void tree_display::paintEvent(QPaintEvent*)
{
    QPainter* painter=new QPainter(this);
    int x = 1000, y =100;
    if (tree != nullptr)
        display(painter, tree->get_root(), x, y);
    delete painter;
}
int min(int a, int b) {
    return a > b ? b : a;
}
int max(int a, int b) {
    return a > b ? a : b;
}
void tree_display::display(QPainter* painter,Node* t,int x,int y) {
    if (t == nullptr) {
        return;
    }
    else {
        painter->drawEllipse(QPoint(x, y), 10, 10);
        int x1 = x, y1 = y;
        QString s=QString::number(t->get_val());
        painter->drawText(QPoint(x-6, y+5), s);
        int x_offset = 8;
        int y_offset = 20;
        int n = t->get_high()-1;
        int distance = pow(2,n);
        double z = x_offset * distance * x_offset * distance + y_offset * y_offset;
        z = pow(z, 0.5);
        double x_p = 10 * ((x_offset * distance) / z);
        double y_p = 10 * ((y_offset) / z);
        if (t->get_left() != nullptr&& t->get_left()->get_key()!=-1) {
            painter->drawLine(x - x_p, y + y_p, x - x_offset * distance + x_p, y + y_offset - y_p);
            display(painter, t->get_left(), x - x_offset * distance, y + y_offset);
        }
        if (t->get_right() != nullptr&&t->get_right()->get_key() != -1) {
            painter->drawLine(x+x_p, y+y_p, x + x_offset * distance-x_p, y + y_offset-y_p);
            display(painter, t->get_right(), x + x_offset * distance, y+ y_offset);
        }
    }
}
void tree_display::Binary_init()
{
    delete tree;
    tree = new Binarytree;
    update();
}

void tree_display::Bserch_init()
{
    delete tree;
    tree = new BSerchTree;
    update();
}

void tree_display::AVL_init()
{
    delete tree;
    tree = new AVLTree;
    update();
}

void tree_display::RBTree_init()
{
    delete tree;
    tree = new RBTree;
    update();
}

void tree_display::b_insert()
{
    int num = q_insert->text().toInt();
    if (tree != nullptr) {
        tree->insert(num, num);
    }
    q_insert->clear();
    update();
}

void tree_display::b_delete()
{
    int num = q_delete->text().toInt();
    if (tree != nullptr) {
        tree->delete_by_key(num);
    }
    q_delete->clear();
    update();
}
